package com.example.applyforaid.service.impl;

import com.example.applyforaid.entity.ManagerUser;
import com.example.applyforaid.entity.StudentUser;
import com.example.applyforaid.func.MessageEncryption;
import com.example.applyforaid.mapper.StudentUserMapper;
import com.example.applyforaid.service.IStudentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ATO3_W
 * @since 2021-06-14
 */
@Service
public class StudentUserServiceImpl implements IStudentUserService {

    @Autowired
    private StudentUserMapper studentUserMapper;

    public int multiUserAccountImport(List<List> src) {
        switch (src.size()){
            case 2:
                return multiImportById(src);
            case 4:
                return multiImportByIdWithPovertyLevel(src);
        }
        return 0;
    }

    private int multiImportByIdWithPovertyLevel(List<List> src) {
        List ids = src.get(0);
        List genders = src.get(1);
        List povertyLevels = src.get(2);
        List povertyVerifyDate = src.get(3);
        for(int i = 0; i < ids.size();i++)
        {
            String uid = (String)ids.get(i);
            String defaultPwd = uid.substring(uid.length()-7, uid.length()-1);
            StudentUser zzy = new StudentUser();
            zzy.setStuUid(uid);
            zzy.setStuKey(MessageEncryption.getSHA256(uid+defaultPwd));
            zzy.setStuGender((genders.get(i)).equals("男"));
            zzy.setStuPovertyLevel((Integer)povertyLevels.get(i));
            studentUserMapper.insert(zzy);
        }
        return 0;
    }

    private int multiImportById(List<List> src) {
        List ids = src.get(0);
        List genders = src.get(1);
        int successCount = 0;
        for(int i = 0; i < ids.size();i++)
        {
            String uid = (String)ids.get(i);
            String defaultPwd = uid.substring(uid.length()-7, uid.length()-1);
            StudentUser zzy = new StudentUser();
            zzy.setStuUid(uid);
            zzy.setStuKey(MessageEncryption.getSHA256(uid+defaultPwd));
            zzy.setStuGender((genders.get(i)).equals("男"));
            zzy.setStuPovertyLevel(1);
            studentUserMapper.insert(zzy);
        }
        return successCount;
    }

    public boolean userLoginVerifyService(String uid,String pwd){
        return (MessageEncryption.getSHA256(uid+pwd).equals(studentUserMapper.selectByPrimaryKey(uid).getStuKey()));
    }

    public boolean userForgetPasswordService(String uid,String contact){
        return (contact.equals(studentUserMapper.selectByPrimaryKey(uid).getStuEmail()));
    }

    public int changeUserPasswordService(String uid,String newPwd){
        try {
            studentUserMapper.updatePasswordByPrimaryKey(uid,MessageEncryption.getSHA256(uid+newPwd));
        }
        catch (Exception e)
        {
            return 0;
        }
        return 1;
    }

    public int deleteUserService(String uid){
        try {
            studentUserMapper.deleteByPrimaryKey(uid);
        }
            catch (Exception e){
            return 0;
            }
        return 1;
    }

    public int createUserService(StudentUser src){
        try{
            studentUserMapper.insert(src);
        }
        catch (Exception e){
            return 0;
        }
        return 1;
    }


    @Override
    public StudentUser getUserAndVerifyService(String uid, String pwd) {
        StudentUser su;
        try{
            su = studentUserMapper.selectByPrimaryKey(uid);
        }
        catch (Exception e)
        {
            return null;
        }
        if(su.getStuKey().equals(MessageEncryption.getSHA256(uid+pwd)))
            return su;
        else
            return null;

    }
}
